package in.muton.edushare.panels.assignment;

import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.components.messaging.api.AssignmentMessage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;
import in.muton.panels.paginator.EdusharePageNavigator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.CheckGroupSelector;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigation;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.AssignmentsDAO;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Messages;

public class AssignmentListPanel extends EdusharePanel {

	private static final long serialVersionUID = -789237480446243294L;
	private IModel<CourseInstances> courseInstanceModel; 
	private static Log log = LogFactory.getLog(AssignmentListPanel.class);
	
	public AssignmentListPanel(String id,CayenneModel<CourseInstances> courseInstanceModel) {
		super(id, courseInstanceModel);
		this.courseInstanceModel = courseInstanceModel;
		add(CSSPackageResource.getHeaderContribution(AssignmentListPanel.class, "assignment.css"));
		loadPanel();
	}

	private void loadPanel() {
		
//		final CourseRoleCheck roleChecker = new CourseRoleCheck(this.courseInstanceModel.getObject(), getLoggedInUser());
		
		WebMarkupContainer asnListCont = new WebMarkupContainer("asnListCont");
		asnListCont.setOutputMarkupId(true);
		add(asnListCont);
		Form<Void> form = new Form<Void>("assnForm");
		asnListCont.add(form);
		final CheckGroup<Assignments> assnGroup = new CheckGroup<Assignments>("assnGroup", new ArrayList<Assignments>());
		Button deleteButton = 
			new Button("deleteSelected"){

				private static final long serialVersionUID = -8209310939557457432L;
	
				@Override
				public void onSubmit() {
					Collection<Assignments> selItems = assnGroup.getModelObject();
					Iterator<Assignments> it = selItems.iterator();
					while(it.hasNext()){
						Assignments assn = it.next();
						it.remove();
						new AssignmentMessage(assn,
								AssignmentMessage.ASSIGNMENT_CANCELLED).setReciever(
								assn.getCourseInstance()).setPrivacy(
								Messages.PRIVACY_TYPE_NONE).send();
						new AssignmentsDAO(getORMInterface()).deleteAssignmentsCompletly(assn);
					}
					
					getORMInterface().commitAllChanges();
				}
			};
			
		if(		!CourseRoleCheck.isAdmin(this.courseInstanceModel.getObject(), getLoggedInUser()) && 
				!CourseRoleCheck.isTeacher(this.courseInstanceModel.getObject(), getLoggedInUser())
		){
			deleteButton.setVisible(false);
		}
		form.add(deleteButton);
		form.add(assnGroup);
		CheckGroupSelector selectAll = new CheckGroupSelector("selectAll", assnGroup);
		if(		!CourseRoleCheck.isAdmin(this.courseInstanceModel.getObject(), getLoggedInUser()) && 
				!CourseRoleCheck.isTeacher(this.courseInstanceModel.getObject(), getLoggedInUser())
		){
			selectAll.setVisible(false);
		}
		assnGroup.add(selectAll);
		
		final WebMarkupContainer noItemDiv = new WebMarkupContainer("noItemDiv");
		PageableListView<IModel<Assignments>> asnList = 
			new PageableListView<IModel<Assignments>>
				(
						"asnList", 
						new PropertyModel<List<IModel<Assignments>>>(AssignmentListPanel.this, "assignmentList"), 
						ApplicationConstants.PAGE_SIZE
				) {

				private static final long serialVersionUID = 4893935510739184188L;

				@Override
				protected void populateItem(ListItem<IModel<Assignments>> item) {
					Check<Assignments> check = new Check<Assignments>("assnCheck", item.getModelObject());
					if(		!CourseRoleCheck.isAdmin(AssignmentListPanel.this.courseInstanceModel.getObject(), getLoggedInUser()) && 
							!CourseRoleCheck.isTeacher(AssignmentListPanel.this.courseInstanceModel.getObject(), getLoggedInUser())
					){
						check.setVisible(false);
					}
					item.add(check);
					PageParameters asnPageParams = new PageParameters();
					asnPageParams.add("0", ""+item.getModelObject().getObject().getAssignmentId());
					BookmarkablePageLink<Void> assnPageLink = 
						new BookmarkablePageLink<Void>("assnPageLink", URLLoader.getPageClass(URLConstants.ASSIGNMENT_PATH), asnPageParams);
					assnPageLink.add(new Label("assnName", new PropertyModel<String>(item.getModelObject(), "assignmentTitle")).setRenderBodyOnly(true));
					item.add(assnPageLink);
					
					EduShareURLPanel courseURLPanel = new EduShareURLPanel
														("assnCourseLink", 
														 new CayenneModel<Courses>(item.getModelObject().getObject()
																 					.getCourseInstance()
																 					.getCourse())
														);
					item.add(courseURLPanel);
					
					item.add(new Label("assnStartDate", new PropertyModel<String>(item.getModelObject(), "formattedStartDate")));
					item.add(new Label("assnEndDate", new PropertyModel<String>(item.getModelObject(), "formattedEndDate")));
					item.add(new Label("assnStatus", new PropertyModel<String>(item.getModelObject(), "statusAsString")));
				}
				
				@Override
					protected void onBeforeRender() {
						if(this.getList()==null || this.getList().size()<1){
							noItemDiv.setVisible(true);
						}else{
							noItemDiv.setVisible(false);
						}
						super.onBeforeRender();
					}
			};
		assnGroup.add(asnList);
		assnGroup.add(noItemDiv);
		assnGroup.add(new EdusharePageNavigator("asnPagination", asnList));
	}
	
	public List<IModel<Assignments>> getAssignmentList(){
		CourseRoleCheck roleChecker = new CourseRoleCheck(this.courseInstanceModel.getObject(), getLoggedInUser());
		List<Assignments> al = null;
		if(roleChecker.isAdmin() || roleChecker.isTeacher()){
			al = new AssignmentsDAO(getORMInterface())
					.getAssignments(
							this.courseInstanceModel.getObject().getCourseInstanceId(), 
							ApplicationConstants.PAGE_SIZE, 
							Assignments.LATE_SUBM_DATE_PROPERTY, 
							ApplicationConstants.SORT_ORDER_DESCENDING);
		}else{
			al = new AssignmentsDAO(getORMInterface()).getNonDraftAssignments(this.courseInstanceModel.getObject());
		}
		ArrayList<IModel<Assignments>> ml = new ArrayList<IModel<Assignments>>(); 
		for(Assignments a: al){
			ml.add(new CayenneModel(a));
		}
		
		
		return ml;
	}

}
